/*
* LibraryDaoImpl.java
* Created on  2014-7-30 下午2:37
* 版本       修改时间          作者      修改内容
* V1.0.1    2014-7-30       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.task.dao.impl;

import com.genghis.core.page.dao.PageDao;
import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.note.entity.Note;
import com.genghis.ptas.task.dao.LibraryDao;
import com.genghis.ptas.task.entity.LibraryOption;
import com.genghis.ptas.task.entity.LibraryTask;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 类的描述信息
 *
 * @author fengxuanzhen
 * @version 1.0.1
 */
@Repository("libraryDao")
public class LibraryDaoImpl extends PageDao implements LibraryDao {

    @Override
    public Page<LibraryTask> findLibraryTaskPageList(PageParameter pageParameter, LibraryTask libraryTask) {
        StringBuilder sql = new StringBuilder("SELECT * FROM library_task where 1=1 ");
        if (libraryTask.getTitle() != null && !libraryTask.getTitle().equals("")) {
            sql.append(" AND title like '%").append(libraryTask.getTitle().trim()).append("%'");
        }
        if (libraryTask.getKnowledgePoint() != null && !libraryTask.getKnowledgePoint().equals("")) {
            sql.append(" AND knowledge_point like '%").append(libraryTask.getKnowledgePoint().trim()).append("%'");
        }
        if (libraryTask.getTaskType() != null && !libraryTask.getTaskType().equals("")) {
            sql.append(" AND task_type = '").append(libraryTask.getTaskType()).append("'");
        }
        if (libraryTask.getMemo() != null && !libraryTask.getMemo().equals("")) {
            sql.append(" AND memo like '%").append(libraryTask.getMemo().trim()).append("%'");
        }
        return queryForPage(pageParameter, sql.toString(), new BeanPropertySqlParameterSource(libraryTask), new BeanPropertyRowMapper<LibraryTask>(LibraryTask.class));
    }

    @Override
    public void deleteLibraryTaskById(Integer id) {
        final String sql = "DELETE FROM library_task WHERE id = ?";
        getJdbcTemplate().update(sql, id);
    }

    @Override
    public void deleteLibraryOptionByTaskId(Integer taskId) {
        final String sql = "DELETE FROM library_option WHERE task_id = ?";
        getJdbcTemplate().update(sql, taskId);
    }

    @Override
    public int addLibraryTask(LibraryTask libraryTask) {
        String sql = "INSERT INTO library_task(title,task_type,option_number,answer,knowledge_point,memo,up_date) " +
                "VALUES(:title, :taskType, :optionNumber, :answer, :knowledgePoint, :memo, :upDate)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(libraryTask), keyHolder);
        return keyHolder.getKey().intValue();
    }

    @Override
    public int addLibraryOption(LibraryOption libraryOption) {
        String sql = "INSERT INTO library_option(option_content,task_id)VALUES(:optionContent, :taskId)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(libraryOption), keyHolder);
        return keyHolder.getKey().intValue();
    }

    @Override
    public LibraryTask findLibraryTaskById(int taskId) {
        String sql = "SELECT * FROM library_task WHERE id = ?";
        return getJdbcTemplate().queryForObject(sql, new BeanPropertyRowMapper<LibraryTask>(LibraryTask.class), taskId);
    }

    @Override
    public List<LibraryOption> findLibraryOptionByTaskId(int taskId) {
        String sql = "SELECT * FROM library_option WHERE task_id = ?";
        return getJdbcTemplate().query(sql, new BeanPropertyRowMapper<LibraryOption>(LibraryOption.class), taskId);
    }

    @Override
    public void editLibraryTask(LibraryTask libraryTask) {
        String sql = "UPDATE library_task SET title = :title, task_type = :taskType , answer = :answer, knowledge_point = :knowledgePoint WHERE id = :id";
        getNamedParameterJdbcTemplate().update(sql, new BeanPropertySqlParameterSource(libraryTask));
    }
}
